package base;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;

public class Connector extends JFrame {
    private static String[] drivers = {"com.mysql.cj.jdbc.Driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver"};
    private static String host = "localhost";
    private static String port = "3306";
    private static String user;
    private static String password;
    private static int dbType;
    private static Connector connector = new Connector();
    private JTextField hostField;
    private JTextField userField;
    private JPasswordField passwordField;
    private JTextField portField;
    private JComboBox<String> jComboBox;
    private Connection conn;
    private static MainFrame mainFrame;

    public static void showFrame(MainFrame mainFrame) {
        connector.hostField.setText(host);
        connector.userField.setText(user);
        connector.passwordField.setText(password);
        connector.portField.setText(port);
        connector.jComboBox.setSelectedIndex(connector.dbType);
        Connector.mainFrame = mainFrame;
        connector.setVisible(true);
    }

    private Connector() {
        setSize(300, 240);
        setLocationRelativeTo(null);
        setResizable(false);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if (mainFrame == null) {
                    if(conn != null){
                        try {
                            conn.close();
                        } catch (SQLException e1) {
                        }
                    }
                    System.exit(0);
                }
                setVisible(false);
            }
        });
        setLayout(new BorderLayout());

        JPanel center = new JPanel();
        center.setLayout(new BorderLayout());
        getContentPane().add(center, BorderLayout.CENTER);

        JLabel label = new JLabel("主机地址");
        JLabel label1 = new JLabel("用户名");
        JLabel label2 = new JLabel("密码");
        JLabel label3 = new JLabel("端口");
        JLabel label4 = new JLabel("数据库");
        label.setFont(new Font("微软雅黑", Font.PLAIN, 17));
        label1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
        label2.setFont(new Font("微软雅黑", Font.PLAIN, 17));
        label3.setFont(new Font("微软雅黑", Font.PLAIN, 17));
        label4.setFont(new Font("微软雅黑", Font.PLAIN, 17));

        JPanel left = new JPanel();
        left.setLayout(new GridLayout(5, 1));
        center.add(left, BorderLayout.WEST);
        left.add(label);
        left.add(label1);
        left.add(label2);
        left.add(label3);
        left.add(label4);

        hostField = new JTextField();
        hostField.setPreferredSize(new Dimension(200, 30));
        userField = new JTextField();
        passwordField = new JPasswordField();
        portField = new JTextField();
        String[] dbTypes = {"MySql", "Microsoft Sql Server"};
        jComboBox = new JComboBox<>(dbTypes);
        jComboBox.setFont(new Font("宋体", Font.PLAIN, 17));
        jComboBox.setSelectedIndex(1);
        JPanel right = new JPanel();
        right.setLayout(new GridLayout(5, 1));
        center.add(right, BorderLayout.EAST);
        right.add(hostField);
        right.add(userField);
        right.add(passwordField);
        right.add(portField);
        right.add(jComboBox);

        JPanel bottom = new JPanel();
        getContentPane().add(bottom, BorderLayout.SOUTH);
        bottom.setLayout(new BorderLayout());
        JButton jButton = new JButton("连接");
        jButton.setPreferredSize(new Dimension(80, 36));
        jButton.setFont(new Font("微软雅黑", Font.PLAIN, 17));
        jButton.setFocusPainted(false);
        bottom.add(jButton, BorderLayout.EAST);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String host = hostField.getText();
                String user = userField.getText();
                String port = portField.getText();
                String password = new String(passwordField.getPassword());
                int dbType = jComboBox.getSelectedIndex();
                if (host == null || host.length() == 0 || user == null || user.length() == 0
                        || port == null || port.length() == 0 || password == null || password.length() == 0) {
                    return;
                }
                try {
                    DriverManager.setLoginTimeout(4);
                    Connection connection = conn;
                    if (dbTypes[dbType].equals("MySql")) {
                        try {
                            Class.forName(drivers[0]);
                        } catch (ClassNotFoundException e1) {
                            JOptionPane.showMessageDialog(null, e1.getMessage(), "警告", JOptionPane.ERROR_MESSAGE);
                            System.exit(0);
                        }
                        conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", user, password);
                    } else if (dbTypes[dbType].equals("Microsoft Sql Server")) {
                        try {
                            Class.forName(drivers[1]);
                        } catch (ClassNotFoundException e1) {
                            JOptionPane.showMessageDialog(null, e1.getMessage(), "警告", JOptionPane.ERROR_MESSAGE);
                            System.exit(0);
                        }
                        conn = DriverManager.getConnection("jdbc:sqlserver://" + host + ":" + port, user, password);
                    }
                    if (connection != null) {
                        connection.close();
                    }
                    Connector.host = host;
                    Connector.user = user;
                    Connector.port = port;
                    Connector.password = password;
                    Connector.dbType = dbType;
                    if (mainFrame != null) {
                        mainFrame.dispose();
                    }
                    connector.setVisible(false);
                    new MainFrame().setVisible(true);
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, "连接失败", "提示", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
            }
        });
    }

    public static int getDbType(){
        return dbType;
    }

    public static Connection getConn() {
        return connector.conn;
    }

}
